using System;
using System.Web.Mvc;
using SapaFinance.DTO;
using SapaFinance.Services;
using Telerik.Web.Mvc;
using Microsoft.Web.Mvc;

namespace SapaFinance.UI.Controllers
{
    public class CfoController : Controller
    {
        private readonly ICfoService _cfoService;

        public CfoController(ICfoService cfoService)
        {
            _cfoService = cfoService;
        }

        public virtual ActionResult RenderCfo(Guid id)
        {
            return View("EditorTemplates/CfoList", _cfoService.GetSelectedListById(id));
        }

        public virtual ActionResult RenderCfoTree(Guid id)
        {
            var z = _cfoService.GetCfoTreeByCorporationId(id);
            return View("CfoTree", z);
        }

        public virtual ActionResult Index()
        {
            return View("Index", _cfoService.GetListDTO());
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Insert(CfoDTO dto)
        {
            if (ModelState.IsValid)
            {
                _cfoService.Insert(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();

        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Delete(Guid id)
        {
            _cfoService.Delete(id);
            return this.RedirectToAction(x => x.Index());
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Update(CfoDTO dto)
        {
            if (ModelState.IsValid)
            {
                _cfoService.Update(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();
        }


    }
}